package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.dto.ProductDetailVO;
import com.genesis.mall.entity.*;
import com.genesis.mall.mapper.PmsProductMapper;
import com.genesis.mall.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@Service
public class PmsProductServiceImpl extends ServiceImpl<PmsProductMapper, PmsProduct> implements IPmsProductService {

    @Autowired
    private  PmsProductMapper pmsProductMapper;

    @Autowired
    private IPmsBrandService brandService;

    @Autowired
    private IPmsProductAttributeService productAttributeService;

    @Autowired
    private IPmsProductAttributeValueService productAttributeValueService;

    @Autowired
    private IPmsSkuStockService skuStockService;

    @Autowired
    private ISmsCouponService smsCouponService;


    @Override
    public ProductDetailVO getProductDetail(Long id) {
        //获取商品信息
        PmsProduct pmsProduct = getById(id);
        //获取该商品的品牌信息
        PmsBrand pmsBrand = brandService.getById(pmsProduct.getBrandId());
        //查找该商品属性参数
        Long productAttributeCategoryId = pmsProduct.getProductAttributeCategoryId();
        LambdaQueryWrapper<PmsProductAttribute> attributeLqw = new LambdaQueryWrapper<>();
        attributeLqw.eq(PmsProductAttribute::getProductAttributeCategoryId, productAttributeCategoryId);
        List<PmsProductAttribute> productAttributeList = productAttributeService.list(attributeLqw);
        //获取存储产品参数信息
        LambdaQueryWrapper<PmsProductAttributeValue> attributeValueLqw = new LambdaQueryWrapper<>();
        attributeValueLqw.eq(PmsProductAttributeValue::getProductId, pmsProduct.getId());
        List<PmsProductAttributeValue> productAttributeValueList = productAttributeValueService.list(attributeValueLqw);
        //获取sku库存
        LambdaQueryWrapper<PmsSkuStock> pmsSkuStockLqw = new LambdaQueryWrapper<>();
        pmsSkuStockLqw.eq(PmsSkuStock::getProductId, pmsProduct.getId());
        List<PmsSkuStock> skuStockList = skuStockService.list(pmsSkuStockLqw);
        //获取优惠券
        LambdaQueryWrapper<SmsCoupon> smsCouponLqw = new LambdaQueryWrapper<>();
        LocalDateTime now = LocalDateTime.now(); //当前时间
        //大于等于开始时间 小于等于截止时间
        smsCouponLqw.ge(SmsCoupon::getStartTime, now).le(SmsCoupon::getEndTime, now);
        List<SmsCoupon> couponList = smsCouponService.list(smsCouponLqw);
        //封装对象
        ProductDetailVO productDetailVO = new ProductDetailVO();
        productDetailVO.setProduct(pmsProduct);
        productDetailVO.setBrand(pmsBrand);
        productDetailVO.setProductAttributeList(productAttributeList);
        productDetailVO.setProductAttributeValueList(productAttributeValueList);
        productDetailVO.setSkuStockList(skuStockList);
        productDetailVO.setCouponList(couponList);

        return productDetailVO;
    }

    @Override
    public PageDto<PmsProduct> search(String keyword, Integer pageNum, Integer pageSize, Integer sort,
                                      Long brandId,Long productCategoryId) {

        Page<PmsProduct> pmsProductPage = new Page<>(pageNum, pageSize);
        PageDto<PmsProduct> pmsProductPageDto = new PageDto<>();
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.like(PmsProduct::getName,keyword);
        if (sort == 0) { //默认排序
           lqw.orderByDesc(PmsProduct::getRecommandStatus);
        } else if (sort == 1) { //按新品排序
            lqw.orderByDesc(PmsProduct::getNewStatus);
        } else if (sort == 2) { //按销量排序
            lqw.orderByDesc(PmsProduct::getSale);
        } else if (sort == 3) { //按价格升序
            lqw.orderByAsc(PmsProduct::getPrice);
        } else if (sort == 4) { //按价格降序
            lqw.orderByDesc(PmsProduct::getPrice);
        }
        if (brandId!=null){
            lqw.eq(PmsProduct::getBrandId,brandId);
        }
        if (productCategoryId!=null){
            lqw.eq(PmsProduct::getProductCategoryId,productCategoryId);
        }
        //分页查询
        this.page(pmsProductPage, lqw);
        pmsProductPageDto.setPageNum(pageNum);
        pmsProductPageDto.setPageSize(pageSize);
        pmsProductPageDto.setTotal((int) pmsProductPage.getTotal());
        pmsProductPageDto.setTotalPage((int) pmsProductPage.getPages());
        pmsProductPageDto.setList(pmsProductPage.getRecords());
        return pmsProductPageDto;
    }

    @Override
    public PageDto<PmsProduct> pageList(Long brandId, Integer pageNum, Integer pageSize) {
        Page<PmsProduct> pmsProductPage = new Page<>(pageNum, pageSize);
        PageDto<PmsProduct> pmsProductPageDto = new PageDto<>();
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProduct::getBrandId,brandId);
        //分页查询
        page(pmsProductPage,lqw);
        pmsProductPageDto.setPageNum(pageNum);
        pmsProductPageDto.setPageSize(pageSize);
        pmsProductPageDto.setTotal((int) pmsProductPage.getTotal());
        pmsProductPageDto.setTotalPage((int) pmsProductPage.getPages());
        pmsProductPageDto.setList(pmsProductPage.getRecords());
        return pmsProductPageDto;

    }

    @Override
    public List<PmsProductCategory> groupBy(Long parentId) {
        List<PmsProductCategory> l=  pmsProductMapper.groupByParentId(parentId);
        return l;
    }
}
